package com.tongji.seller.model
{
	import alternativa.protocol.codec.NullMap;
	
	import com.adobe.serialization.json.JSON;
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.view.Work;
	import com.tongji.seller.view.twoD.Furniture;
	import com.tongji.seller.vo.FurnitureSaved;
	
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author frank
	 * 2011/09/02
	 * 实现对场景的保存
	 */
	[Bindable]
	public class UserSceneModel extends Actor
	{
		[Inject]
		public var userModel:UserModel;

		public var enable:Boolean = false;
		public var saveSceneArray:ArrayCollection;//该变量存储了多个场景内容
		public var currentScene:Object;
		public var userProduct:Object;
		
		public var scene:String;
		public var sceneArr:ArrayCollection;
		public var getUserSceneArr:ArrayCollection;
		
		//caddy
		public var currentPosition:Point;
		public var currentFurniture:Furniture;
		public var userAngles:Array = new Array();
		public var userSceneArray:ArrayCollection = new ArrayCollection();
		
		public var images:ArrayCollection;
		
		public var currentUserScene:Object = new Object();
		
		public var recordCount:Number;
		public var sceneCount:Number;
		public var pageIndex:Number = 1;
		
		public function UserSceneModel()
		{
			super();
		}
		
		public function updateUserScene(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			getUserSceneArr = new ArrayCollection(sceneArr[0].VR_SceneList);
			recordCount = sceneArr[0].PageCount;
		}
		/**
		 * @author caddy 
		 * @param images
		 * 
		 */		
		public function updateImage(images:ArrayCollection):void
		{
			this.images = images;
		}
		
		

	
		
		/**保存用户自定义的场景
		 * @author caddy 
		 * @param currentScene
		 * @param userProduct
		 * 
		 */		
		public function saveScene(currentScene:Object, userProduct:Object = null):void
		{
			this.currentScene = currentScene;
			var userScene:Object = new Object();
			userScene.ImgUrl = "";
			userScene.state = 1;
			userScene.uId = "22";
			var SceneAngleList:Object = [];
			var angleCount:int = currentScene.SceneAngleList.length;
			var angle:Object;
			var info:Object;
			for (var i:int = 0; i < angleCount; i++)
			{
				angle = new Object();
				var currentAngle:Object = currentScene.SceneAngleList[i];
				angle.Asid = currentAngle.Asid;
				var infoCount:int = currentAngle.SceneInfoList.length;
				var SceneInfoList:Object = [];
				for (var k:int = 0; k < infoCount; k++)
				{
					info = new Object();
					var currentInfo:Object = currentAngle.SceneInfoList[k];
					info.siId = currentInfo.siId;
					info.State = currentInfo.State;
					SceneInfoList[k] = info;
				}
				angle.SceneInfoList = SceneInfoList;
				SceneAngleList[i] = angle;
			}
			userScene.SceneAngleList = SceneAngleList;
			
			var userScentString:String = JSON.encode(userScene);
		
		}
		
		public  function save(currentScene:Object, userAngles:Array):void
		{
			//var scene:UserScene = new UserScene();
			var userScene:Object = new Object();
			userScene.ImgUrl = images[0];
			userScene.uId = userModel.currentUserID;
			userScene.sid = currentScene.Sourceid;
			userScene.sceneName = currentScene.sceneName;
			var SceneAngleList:Object = userAngles;
			var angle:Object;
			var info:Object;
			var SceneInfoList:Object = [];
			for(var i:int = 0; i < userAngles.length; i++)
			{
				userAngles[i].Bgurl = images[i];
			}
			userScene.SceneAngleList = SceneAngleList;
			var userScentString:String = JSON.encode(userScene);
			dispatch(new AddStoreSceneEvent(AddStoreSceneEvent.ADD_STORE_USER_SCENE, userModel.currentUserID, 0, null, userScentString));
		}
		
		public function saveAngleHandler(work:Work, angle:Object):Object
		{
			//1kejian ,0 bukejian
			var userAngle:Object = new Object();			
			var info:Object;
			var SceneInfoList:Array = new Array();
			
			userAngle.Jdid = angle.Jdid;

			for each(var obj:Object in work.hideFurnitureList.dataProvider)
			{
				info = new Object();
				info.depth = obj.depth;
				info.pId = obj.pId;
				info.xAxis = obj.xAxis;
				info.yAxis = obj.yAxis;
				if (obj.sceneInfoBitmap.visible)
					info.state = 1;
				else
					info.state = 0;
				info.Extend = "";
				SceneInfoList.push(info);
				
			}
			
			for(var a:int = 0; a < work.roomDisplay.numChildren; a++)
			{
				if (work.roomDisplay.getChildAt(a) is Furniture)
				{
					info = new Object();
					info.pId = (work.roomDisplay.getChildAt(a) as Furniture).hostProduct.pid;
					var fur:Furniture = work.roomDisplay.getChildAt(a) as Furniture;
				
					info.xAxis = fur.x;
					info.yAxis = fur.y;
					info.depth = fur.depth;
					info.state = 1;
					var furInfo:FurnitureSaved = new FurnitureSaved();
					var handles:Array = new Array();
					handles[0] = fur.handle0;
					handles[1] = fur.handle1;
					handles[2] = fur.handle2;
					handles[3] = fur.handle3;
					handles[4] = fur.handle4;
					handles[5] = fur.handle5;
					handles[6] = fur.handle6;
					handles[7] = fur.handle7;
					
					furInfo.handles = handles;
					if (fur.filter != null)
					{
						furInfo.filter = fur.filter;
					}
					furInfo.bright = fur.bright.blueOffset;
					furInfo.url = fur.getUrl();
					info.Extend = furInfo.saveFurInfo();
					
					SceneInfoList.push(info);
				}					
			}
			userAngle.SceneInfoList = SceneInfoList;
			
			return userAngle;
		}
	}
}